package Arrays;

public class _849_MaximizeDistancetoClosestPerson {
    //hint:be care of the beginning's 0 and the end's 0
    //just do judge when seats[x]=1
    public int maxDistToClosest(int[] seats) {
        if (seats.length == 1) {
            return 1;
        }
        int maxDistance = 0;
        int tempDistance = 0;
        boolean isEmpty = true;
        //find out if the seat is empty from the beginning
        if (seats[0] == 0) {
            isEmpty = true;
        } else {
            isEmpty = false;
        }
        for (int i = 1; i < seats.length-1; i++) {
            tempDistance++;
            if (seats[i] != 0) {
                if (isEmpty) {
                    if (tempDistance > maxDistance) {
                        maxDistance = tempDistance;
                    }
                    isEmpty = false;
                } else {
                    if ((tempDistance / 2) > maxDistance) {
                        maxDistance = (tempDistance / 2);
                    }
                }
                tempDistance = 0;
            }
        }
        tempDistance++;
        if (seats[seats.length - 1] == 0) {
            if (tempDistance > maxDistance) {
                maxDistance = tempDistance;
            }
        } else {
            if (isEmpty) {
                if (tempDistance > maxDistance) {
                    maxDistance = tempDistance;
                }
                isEmpty = false;
            } else {
                if ((tempDistance / 2) > maxDistance) {
                    maxDistance = (tempDistance / 2);
                }
            }
        }
        return maxDistance;
    }
}
